#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "MyString.h"
#include "DynamicArray.h"
#include "Tool.h"

void InitPlayer(struct Player *player)
{
    player->HP = 100;
    player->attack = 300;
    player->x = 0;
    player->y = 0;
    player->pocket = 3;
}

// 第1个NPC
struct NonPlayerA *CreateNonPlayerA(const char *name, int value)
{
    struct NonPlayerA *NPC = (struct NonPlayerA *)malloc(sizeof(struct NonPlayerA));
    if (NPC == NULL)
    {
        printf("Create NPC Error!\n");
        return NULL;
    }
    NPC->value = value;
    Initiallize(&NPC->nameA, name);

    NPC->x = GetRandNumber(Map_Size);
    NPC->y = GetRandNumber(Map_Size);
    while (NPC->x == 0 && NPC->y == 0)
    {
        NPC->x = GetRandNumber(Map_Size);
        NPC->y = GetRandNumber(Map_Size);
    }
    return NPC;
}
void InitNonPlayerA(struct DynamicArray *array)
{
    if (ArrayInit(array) == false)
    {
        return;
    }
    InsertArray(array, CreateNonPlayerA("神秘商人", 5));
}
// 第2个NPC
struct NonPlayerB *CreateNonPlayerB(const char *name, int value)
{
    struct NonPlayerB *NPC = (struct NonPlayerB *)malloc(sizeof(struct NonPlayerB));
    if (NPC == NULL)
    {
        printf("Create NPC Error!\n");
        return NULL;
    }
    NPC->value = value;
    Initiallize(&NPC->nameB, name);

    NPC->x = GetRandNumber(Map_Size);
    NPC->y = GetRandNumber(Map_Size);
    while (NPC->x == 0 && NPC->y == 0)
    {
        NPC->x = GetRandNumber(Map_Size);
        NPC->y = GetRandNumber(Map_Size);
    }
    return NPC;
}
void InitNonPlayerB(struct DynamicArray *array)
{
    if (ArrayInit(array) == false)
    {
        return;
    }
    InsertArray(array, CreateNonPlayerB("小妲己", 5));
}
// 第3个NPC
struct NonPlayerC *CreateNonPlayerC(const char *name, int value)
{
    struct NonPlayerC *NPC = (struct NonPlayerC *)malloc(sizeof(struct NonPlayerC));
    if (NPC == NULL)
    {
        printf("Create NPC Error!\n");
        return NULL;
    }
    NPC->value = value;
    Initiallize(&NPC->nameC, name);

    NPC->x = GetRandNumber(Map_Size);
    NPC->y = GetRandNumber(Map_Size);
    while (NPC->x == 0 && NPC->y == 0)
    {
        NPC->x = GetRandNumber(Map_Size);
        NPC->y = GetRandNumber(Map_Size);
    }
    return NPC;
}
void InitNonPlayerC(struct DynamicArray *array)
{
    if (ArrayInit(array) == false)
    {
        return;
    }
    InsertArray(array, CreateNonPlayerC("沸羊羊", 5));
}


void BloodVolume(struct Player *player)
{
    printf("目前的血量:%d,攻击力: %d,小金库 : %d\n", player->HP, player->attack, player->pocket);
    sleep(1);
    if (player->HP > 50)
    {
        printf("还能浪，奥力给！！!\n");
        sleep(1);
        return;
    }
    if (player->HP < 20)
    {
        printf("哥，耶稣也救不了你了，我说的！！!\n");
        sleep(1);
        return;
    }
}

void MakeMove(struct Player *player, char symbol)
{
    switch (symbol)
    {
    case 'w':
        player->x--;
        break;
    case 's':
        player->x++;
        break;
    case 'a':
        player->y--;
        break;
    case 'd':
        player->y++;
        break;
    case 'f':
        BloodVolume(player);
        break;
    case 'o':
        Luckydraw(player);
        break;
    default:
        break;
    }

    if (player->x < 0)
        player->x = 0;
    if (player->x >= Map_Size)
        player->x = Map_Size - 1;
    if (player->y < 0)
        player->y = 0;
    if (player->y >= Map_Size)
        player->y = Map_Size - 1;
}

int Battle(struct Player *player, struct Monster *monster)
{
    system("clear");
    printf("                您遭遇了|%s|血量:|%d|  攻击力:|%d|\n", monster->name.string, monster->HP, monster->attack);
    printf("                你当前血量|%d|攻击力|%d| \n", player->HP, player->attack);
    printf("***************************************************************************************************\n");
    printf("*                                              开始战斗！                                           *\n");
    printf("***************************************************************************************************\n");
    while (player->HP > 0 && monster->HP > 0)
    {
        printf("请选择您要执行的行为: \n");
        printf("1、普通攻击\n");
        printf("2、暴击\n");
        printf("3、逃跑\n");
        int choice;
        scanf("%d", &choice);
        getchar();
        switch (choice)

        {
        case 1:
            system("clear");
            monster->HP -= player->attack;
            printf("                你对|%s|造成了%d伤害\n", monster->name.string, player->attack);
            if (monster->HP > 0)
            {
                player->HP -= monster->attack;
                printf("                |%s|对你造成了%d伤害\n", monster->name.string, monster->attack);
            }
            else
            {
                printf("                怪兽已经阵亡，你真的是泰泰泰泰泰酷啦!!!!!!!!!!!!\n");
            }
            printf("                你当前血量|%d|攻击力|%d| \n", player->HP, player->attack);
            printf("                |%s|当前血量|%d|攻击力|%d| \n", monster->name.string, monster->HP, monster->attack);
            printf("                你当前所在的位置是<%d , %d>\n ", player->x + 1, player->y + 1);
            sleep(1);
            break;

        case 2:
            system("clear");
            if (GetRandNumber(2) == 1)
            {
                monster->HP -= player->attack * 2;
                printf("                鸡哥附体，爆坤成功,你对|%s|造成了%d伤害\n", monster->name.string, player->attack * 2);
                if (monster->HP > 0)
                {
                    player->HP -= monster->attack;
                    printf("                |%s|对你造成了%d伤害\n", monster->name.string, monster->attack);
                }
                else
                {
                    printf("                怪兽已经阵亡，你真的是泰泰泰泰泰酷啦!!!!!!!!!!!\n");
                }
                // printf("                |%s|对你造成了%d伤害\n", monster->name.string, monster->attack);
                // printf("                你对|%s|造成了%d伤害\n", monster->name.string, player->attack * 2);
                // player->HP -= monster->attack;
                // monster->HP -= player->attack * 2;
                printf("                你当前血量|%d|攻击力|%d| \n", player->HP, player->attack);
                printf("                |%s|当前血量|%d|攻击力|%d| \n", monster->name.string, monster->HP, monster->attack);
                printf("                你当前所在的位置是<%d , %d>\n ", player->x + 1, player->y + 1);
                sleep(1);
            }
            else
            {
                printf("                |%s|对你造成了%d伤害\n", monster->name.string, monster->attack);
                printf("                你是真爱粉吗，居然暴击失败!无法发动攻击!\n");
                player->HP -= monster->attack;
                printf("                你当前血量|%d|攻击力|%d| \n", player->HP, player->attack);
                printf("                |%s|当前血量|%d|攻击力|%d| \n", monster->name.string, monster->HP, monster->attack);
                printf("                你当前所在的位置是<%d , %d>\n ", player->x + 1, player->y + 1);
            }
            sleep(1);
            break;

        case 3:
            system("clear");
            if (GetRandNumber(2) == 1)
            {
                printf("                逃跑成功            \n");
                printf("\n");
                switch (GetRandNumber(4))
                {
                case 0:
                    player->x++;
                    printf("                跑得这么快，你是参加奥运会了吗？\n");
                    break;
                case 1:
                    player->x--;
                    printf("                是逃跑还是在进行突击训练?\n");
                    break;
                case 2:
                    player->y++;
                    printf("                看来你的腿比你的勇气更强壮。\n");
                    break;
                case 3:
                    player->y--;
                    printf("                逃跑是你唯一擅长的技能吗？\n");
                    break;
                }
                if (player->x < 0)
                    player->x = 0;
                if (player->x >= Map_Size)
                    player->x = Map_Size - 1;
                if (player->y < 0)
                    player->y = 0;
                if (player->y >= Map_Size)
                    player->y = Map_Size - 1;
                printf("                你当前血量|%d|攻击力|%d| \n", player->HP, player->attack);
                printf("                你当前所在的位置是<%d , %d>\n", player->x + 1, player->y + 1);
                return true;
            }
            else
            {
                printf("逃跑失败!王甜心给你送来祝福!\n");
                printf("                |%s|对你造成了%d伤害\n", monster->name.string, monster->attack);
                player->HP -= monster->attack;
                printf("                你当前血量|%d|攻击力|%d| \n", player->HP, player->attack);
                printf("                你当前所在的位置是<%d , %d>\n ", player->x + 1, player->y + 1);
            }
            break;

        default:
            break;
        }
        printf("========================================end=============================================\n");
        sleep(1);
        if (player->HP <= 0)
        {
            printf("                你被│%s|击败了!\n", monster->name.string);
            player->HP = 0;
            return false;
        }
        if (monster->HP <= 0)
        {
            monster->HP = 0;
            printf("                你击败了|%s | !\n", monster->name.string);
            printf("                你当前血量|%d|攻击力|%d| \n", player->HP, player->attack);
            return true;
        }
    }
}

int Money(struct Player *player, struct Coin *coin)
{
    system("clear");
    printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(^_^)$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
    while (player->HP > 0)
    {
        if (GetRandNumber(4) == 1)
        {
            printf("                天上掉馅饼,鸡哥赐宝|%d|坤\n", coin->value);
            printf("                您获得了|%s| 小金库增加|%d|坤\n", coin->name.string, coin->value);
            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(^_^)$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
            player->pocket += coin->value;
            coin->value -= coin->value;
            printf("你当前存有|%d|坤\n", player->pocket);
            break;
        }
        else if (GetRandNumber(4) == 2)
        {
            printf("                王涵涵送来了一个祝福，奖励|%d|坤\n", coin->value);
            printf("                您获得了|%s| 小金库增加|%d|坤\n", coin->name.string, coin->value);
            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(^_^)$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
            player->pocket += coin->value;
            coin->value -= coin->value;
            printf("                你当前存有|%d|坤\n", player->pocket);
            break;
        }
        else if (GetRandNumber(4) == 3)
        {
            printf("                你被打了个大逼兜，奖励|%d|坤\n", coin->value);
            printf("                您获得了|%s| 小金库增加|%d|坤\n", coin->name.string, coin->value);
            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(^_^)$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
            player->pocket += coin->value;
            coin->value -= coin->value;
            printf("                你当前存有|%d|坤\n", player->pocket);
            break;
        }
        else
        {
            printf("                遇到了外星人，奖励|%d|坤\n", coin->value);
            printf("                您获得了|%s| 小金库增加|%d|坤\n", coin->name.string, coin->value);
            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(^_^)$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
            player->pocket += coin->value;
            coin->value -= coin->value;
            printf("                你当前存有|%d|坤\n", player->pocket);
            break;
        }
    }
    printf("=======================================end==============================================\n");
    sleep(1);
}
//N1
int MissA(struct Player *player, struct NonPlayerA *NPC)
{
    system("clear");
    printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(^_^)$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
    printf("你遇到了%s\n", NPC->nameA.string);
    if (player->HP > 0)
    {
        if (GetRandNumber(3) == 1)
        {
            printf("                好饿好饿好饿我真的好饿！！！\n");
            printf("                你能给我2块钱去买个面包吗?\n");
            sleep(2);
            printf("请选择您要执行的行为: \n");
            printf("1、当然可以!\n");
            printf("2、你想钱想疯了吧\n");
            int choice;
            scanf("%d", &choice);
            getchar();
            switch (choice)
            {
            case 1:
                system("clear");
                if (player->pocket >= 2)
                {
                    player->pocket -= 2;
                    printf("%s: 谢谢你，你比王涵涵好太多了!!我会给你一些奖励!!\n", NPC->nameA.string);
                    player->HP += 5;
                    printf("你的血量增加了5点\n");
                    sleep(1);
                }
                else
                {
                    printf("%s: 你连2块钱都没有,早晚会死在这里!!!\n", NPC->nameA.string);
                    printf("%s踹了你一脚,溜溜球了", NPC->nameA.string);
                    player->HP -= 1;
                    printf("你的血量减少了1点\n");
                    sleep(1);
                }
                break;
            case 2:
                system("clear");
                printf("上帝不会庇佑你了\n");
                sleep(1);
                break;
            default:
                break;
            }
        }
        else if (GetRandNumber(3) == 2)
        {
            printf("                |%s|送来了一个祝福，奖励|%d|坤\n", NPC->nameA.string, NPC->value);
            printf("                您获得了|%s| 小金库增加|%d|坤\n", NPC->nameA.string, NPC->value);
            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(^_^)$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
            player->pocket += NPC->value;
            NPC->value -= NPC->value;
            printf("                你当前存有|%d|坤\n", player->pocket);
            
        }
        else
        {
            printf("                王涵涵假扮成了神秘商人并打劫了你|%d|坤\n", NPC->value);
            printf("                小金库减少|%d|坤\n", NPC->value);
            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(^_^)$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
            player->pocket -= NPC->value;
            NPC->value -= NPC->value;
            printf("                你当前存有|%d|坤\n", player->pocket);
            
        }
    }
    printf("=======================================end==============================================\n");
    sleep(1);
}
//N2
int MissB(struct Player *player, struct NonPlayerB *NPC)
{
    system("clear");
    printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(^_^)$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
    printf("你遇到了%s\n", NPC->nameB.string);
    if (player->HP > 0)
    {
        if (GetRandNumber(3) == 1)
        {
            printf("                妲己好想你吖，主人！！！\n");
            printf("                你能抱抱妲己吗!!!!\n");
            sleep(1);
            printf("请选择您要执行的行为: \n");
            printf("1、当然可以!\n");
            printf("2、你想疯了吧\n");
            int choice;
            scanf("%d", &choice);
            getchar();
            switch (choice)

            {
            case 1:
                system("clear");
                printf("%s: 谢谢你，你太好了!!我会给你一些奖励!!\n", NPC->nameB.string);
                player->HP += 5;
                printf("你的血量增加了5点\n");
                

                break;
            case 2:
                system("clear");
                printf("%s: 你比王涵涵还渣，死渣男!!!\n", NPC->nameB.string);
                printf("%s黑化,对你发动了爱心光波", NPC->nameB.string);
                player->HP -= 10;
                printf("你的血量减少了10点\n");
                
                break;
            default:
                break;
            }
        }

        else if (GetRandNumber(3) == 2)
        {
            printf("                |%s|送来了一个祝福，奖励|%d|坤\n", NPC->nameB.string, NPC->value);
            printf("                您获得了|%s| 小金库增加|%d|坤\n", NPC->nameB.string, NPC->value);
            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(^_^)$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
            player->pocket += NPC->value;
            NPC->value -= NPC->value;
            printf("                你当前存有|%d|坤\n", player->pocket);
            
        }
        else
        {
            printf("                王涵涵突然穿出伤害了妲己并打劫了你|%d|坤\n", NPC->value);
            printf("                血量减少|%d|\n", NPC->value);
            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(^_^)$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
            player->HP -= NPC->value;
            NPC->value -= NPC->value;
            printf("                你当前血量|%d|\n", player->HP);
            
        }
    }
    printf("=======================================end==============================================\n");
    sleep(1);
}
//N3
int MissC(struct Player *player, struct NonPlayerC *NPC)
{
    system("clear");
    printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(^_^)$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
    printf("你遇到了%s\n", NPC->nameC.string);
    if(player->HP > 0)
    {
        if (GetRandNumber(2) == 1)
        {
            printf("                我是沸羊羊，快乐的小羊羊！！！\n");
            printf("                你觉得我是舔狗还是战狼!!!\n");
            sleep(2);
            printf("请选择您要执行的行为: \n");
            printf("1、舔狗!\n");
            printf("2、战狼!\n");
            int choice;
            scanf("%d", &choice);
            getchar();
            switch (choice)
            {
            case 1:
                system("clear");
                printf("%s: 你说的对，王涵涵比我强太多了!!我会给你一些奖励!!\n", NPC->nameC.string);
                player->HP += 10;
                printf("你的血量增加了10点\n");
                sleep(1);
                break;
            case 2:
                system("clear");
                printf("%s: 居然被你发现了，你是怎么知道我是灰太狼假扮的!!!\n", NPC->nameC.string);
                printf("%s踹了你一脚并说我一定会回来的!!", NPC->nameC.string);
                player->HP -= 3;
                printf("你的血量减少了3点\n");
                sleep(1);
                
                break;
            default:
                break;
            }
        }
        else
        {
            printf("                |%s|送来了一个祝福，奖励|%d|坤\n", NPC->nameC.string, NPC->value);
            printf("                您获得了|%s| 小金库增加|%d|坤\n", NPC->nameC.string, NPC->value);
            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(^_^)$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
            player->pocket += NPC->value;
            NPC->value -= NPC->value;
            printf("                你当前存有|%d|坤\n", player->pocket);
        }
    }
    printf("=======================================end==============================================\n");
    sleep(1);
}